%%
%第一小问



% 第一问缺失值处理代码
filename = '数据预处理1.xlsx';
data = readtable(filename);%带了最后的一个算的总和
%%
filename1 = 'C第二次.xlsx';%表格一当中的数据
data2 = readtable(filename1);
%%
data3 = readtable("C第二次.xlsx","Sheet","表单2");%原始表格当中的数据

output_filename = 'modified_data.xlsx';%修改后的文件数据

%纹饰
if ~iscellstr(data2{:,2})
    data2{:,2} = cellstr(data2{:,2});
end

data2{:,2} = regexprep(data2.x__,'A','1');
data2{:,2} = regexprep(data2.x__,'B','2');
data2{:,2} = regexprep(data2.x__,'C','3');

% 检查列是否为字符串类型，如果不是，则转换为字符串类型
%风化与无风化
if isnumeric(data2.x_____1)
    % 将列转换为字符串类型
    data2.x_____1 = num2cell(data2.x_____1);
end

data2.x_____1= strrep(data2.x_____1, '无风化', '0');
data2.x_____1= strrep(data2.x_____1, '风化', '1');

%类型
if isnumeric(data2.x___1)
    data2.x___1 = num2cell(data2.x___1);
end

data2.x___1 = strrep(data2.x___1,'高钾','2');
data2.x___1 = strrep(data2.x___1,'铅钡','1');

%颜色
if isnumeric(data2.x___2)
    data2.x___2 = num2cell(data2.x___2);
end

data2.x___2 = strrep(data2.x___2,'蓝绿','1');
data2.x___2 = strrep(data2.x___2,'浅蓝','2');
data2.x___2 = strrep(data2.x___2,'紫','3');
data2.x___2 = strrep(data2.x___2,'深绿','4');
data2.x___2 = strrep(data2.x___2,'浅绿','5');
data2.x___2 = strrep(data2.x___2,'黑','6');
data2.x___2 = strrep(data2.x___2,'绿','7');
data2.x___2 = strrep(data2.x___2,'深蓝','8');
%%
% 观察表格表格
data2.x__ = str2double(data2.x__);
data2.x_____1 = str2double(data2.x_____1);
data2.x___1 = str2double(data2.x___1);
data2.x___2 = str2double(data2.x___2);

%%
%纹饰表格
wenshi = zeros(2,8);
%计算纹饰表格观察值
wenfeng01=0;wenfeng11=0;wenfeng02=0;
wenfeng12=0;wenfeng03=0;wenfeng13=0;
for i = 1:size(data2(:,1))
    if data2{i,2}==1&&data2{i,end}==0
        wenfeng01 = wenfeng01+1;
    end
    if data2{i,2}==1&&data2{i,end}==1
        wenfeng11 = wenfeng11+1;
    end
    if data2{i,2}==2&&data2{i,end}==0
        wenfeng02 = wenfeng02+1;
    end
    if data2{i,2}==2&&data2{i,5}==1
        wenfeng12 = wenfeng12+1;
    end
    if data2{i,2}==3&&data2{i,5}==0
        wenfeng03 = wenfeng03+1;
    end
    if data2{i,2}==3&&data2{i,5}==1
        wenfeng13 = wenfeng13+1;
    end
end
wenshi(1,1)=wenfeng01;wenshi(1,2)=wenfeng02;wenshi(1,3)=wenfeng03;
wenshi(2,1)=wenfeng11;wenshi(2,2)=wenfeng12;wenshi(2,3)=wenfeng13;
%%
%计算行和与列和
%计算纹饰表格期望值
row_wenshi=sum(wenshi,2);
column_wenshi=sum(wenshi);
zonghe = sum(row_wenshi);
qiwang_wenshi = zeros(2,8);
for i = 1:8
    qiwang_wenshi(1,i) = (row_wenshi(1,1)*column_wenshi(1,i))/zonghe;
end
for i = 1:8
    qiwang_wenshi(2,i)=(row_wenshi(2,1)*column_wenshi(1,i))/zonghe;
end
%频率
o_wenshi=zeros(2,8);
o_wenshi=wenshi;
%%
%纹饰计算卡方
x_juzhenwenshi = ((o_wenshi-qiwang_wenshi).^2)./qiwang_wenshi;
%x= sum(x_juzhen,);
%%
%卡方临界值
a = 0.05;
df = (3-1)*(2-1);
% if x>5.991
%     fprintf('无关不会受到影响');
% else
%     fprintf('两者之间有较强的相关性');
% end
%%
%类型表格  铅钡--1与高钾--2
%类型观察值处理
leixing = zeros(2,8);
leixing01 =0;leixing02=0;
leixing11=0;leixing12=0;
for i = 1:size(data2(:,1))
    if data2{i,3}==1&&data2{i,end}==0
         leixing01 = leixing01+1;
    end
    if data2{i,3}==1&&data2{i,end}==1
        leixing11 = leixing11+1;
    end
    if data2{i,3}==2&&data2{i,end}==0
        leixing02 = leixing02+1;
    end
    if data2{i,3}==2&&data2{i,5}==1
        leixing12 = leixing12+1;
    end
end
leixing(1,1)=leixing01;leixing(1,2)=leixing02;
leixing(2,1)=leixing11;leixing(2,2)=leixing12;
%%
%类型期望值处理
row_leixing=sum(leixing,2);
column_leixing=sum(leixing);
zonghe = sum(row_leixing);
qiwang_leixing = zeros(2,8);
for i = 1:8
    qiwang_leixing(1,i) = (row_leixing(1,1)*column_leixing(1,i))/zonghe;
end
for i = 1:8
    qiwang_leixing(2,i)=(row_leixing(2,1)*column_leixing(1,i))/zonghe;
end
o_leixing=zeros(2,8);
o_leixing=leixing;
%%
%类型计算卡方
x_juzhenleixing = ((o_leixing-qiwang_leixing).^2)./qiwang_leixing;

%%
%卡方临界值
a = 0.05;
df = (2-1)*(2-1);
% if x>5.991
%     fprintf('无关不会受到影响');
% else
%     fprintf('两者之间有较强的相关性');
% end
%%
%颜色表格
%观测值
yanse = zeros(2,8);
yanse01=0;yanse02=0;yanse03=0;yanse04=0;yanse05=0;yanse06=0;yanse07=0;yanse08=0;
yanse11=0;yanse12=0;yanse13=0;yanse14=0;yanse15=0;yanse16=0;yanse17=0;yanse18=0;
for i = 1:size(data2(:,1))
    if data2{i,4}==1&&data2{i,end}==0
        yanse01 = yanse01+1;
    end
    if data2{i,4}==2&&data2{i,end}==0
        yanse02 = yanse02+1;
    end
    if data2{i,4}==3&&data2{i,end}==0
        yanse03 = yanse03+1;
    end
    if data2{i,4}==4&&data2{i,5}==0
        yanse04 = yanse04+1;
    end
    if data2{i,4}==5&&data2{i,5}==0
        yanse05 = yanse05+1;
    end
    if data2{i,4}==6&&data2{i,5}==0
        yanse06 = yanse06+1;
    end
    if data2{i,4}==7&&data2{i,5}==0
        yanse07 = yanse07+1;
    end
    if data2{i,4}==8&&data2{i,5}==0
        yanse08 = yanse08+1;
    end
    if data2{i,4}==1&&data2{i,5}==1
        yanse11 = yanse11+1;
    end
    if data2{i,4}==2&&data2{i,5}==1
        yanse12 = yanse12+1;
    end
    if data2{i,4}==3&&data2{i,5}==1
        yanse13 = yanse13+1;
    end
    if data2{i,4}==4&&data2{i,5}==1
        yanse14 = yanse14+1;
    end
    if data2{i,4}==5&&data2{i,5}==1
        yanse15 = yanse15+1;
    end
    if data2{i,4}==6&&data2{i,5}==1
        yanse16 = yanse16+1;
    end
    if data2{i,4}==7&&data2{i,5}==1
        yanse17 = yanse17+1;
    end
    if data2{i,4}==8&&data2{i,5}==1
        yanse18 = yanse18+1;
    end
end
yanse(1,1)=yanse01;yanse(1,2)=yanse02;yanse(1,3)=yanse03;yanse(1,4)=yanse04;
yanse(1,5)=yanse05;yanse(1,6)=yanse06;yanse(1,7)=yanse07;yanse(1,8)=yanse08;
yanse(2,1)=yanse11;yanse(2,2)=yanse12;yanse(2,3)=yanse13;yanse(2,4)=yanse14;
yanse(2,5)=yanse15;yanse(2,6)=yanse16;yanse(2,7)=yanse17;yanse(2,8)=yanse18;
%%
%期望值
row_yanse=sum(yanse,2);
column_yanse=sum(yanse);
zonghe = sum(row_yanse);
qiwang_yanse = zeros(2,8);
for i = 1:8
    qiwang_yanse(1,i) = (row_yanse(1,1)*column_yanse(1,i))/zonghe;
end
for i = 1:8
   qiwang_yanse(2,i)=(row_yanse(2,1)*column_yanse(1,i))/zonghe;
end
o_yanse=zeros(2,8);
o_yanse=yanse;
%%
%颜色计算卡方
x_juzhenleixing = ((o_yanse-qiwang_yanse).^2)./qiwang_yanse;
x_zuizhong=sum(x_juzhenleixing,"all");








